package com.ciccwm.core.validation.validator;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

import com.ciccwm.core.validation.annotation.IdCard;

/**
 * 身份证号验证器.
 *
 * @author ciccwm
 */
public class IdCardValidator implements ConstraintValidator<IdCard, String> {
    /**
     * 身份证号码正则表达式.
     */
    private static final Pattern ID_CARD_PATTERN = Pattern.compile(
        "^[1-9]\\d{5}(19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\\d|3[01])\\d{3}[0-9Xx]$");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return ID_CARD_PATTERN.matcher(value).matches();
    }
} 
